<?php

class CollectionForm extends BaseForm {

   public function __construct($isCheck=false, $collectionCode=false) {
        $this->required = $isCheck;
        $this->code = $collectionCode;
        parent::__construct();
   }

    public function setup() {
        $customers = $this->getCustomersFormatted();
        $lines = $this->getLinesFormatted();
        
        $this->setWidgets(array(
            'cheque_number' => new sfWidgetFormInputText(),
            'collection_code' => new sfWidgetFormInputText(),
            'bank_name' => new sfWidgetFormInputText(),
            'customer_id' => new sfWidgetFormChoice(array('choices' => $customers)),
            'line_id' => new sfWidgetFormChoice(array('choices' => $lines)),
            'amount' => new sfWidgetFormInputText(),
            'pay_type' =>  new sfWidgetFormChoice(array('choices'  => array('cheque'=>'Cheque', 'cash'=>'Cash', 'transfer'=>'Transfer'),'expanded' => true)),
            'date' => new sfWidgetFormInputText(),
            'due_date' => new sfWidgetFormInputText()
            ));


        $this->setValidators(array(
            'cheque_number' => new sfValidatorNumber(array('required' => $this->required), array('required' => 'Please enter required field.')),
            'collection_code' => new sfValidatorString(array('required' => true), array('required' => 'Please enter required field.')),
            'bank_name' => new sfValidatorString(array('required' => $this->required), array('required' => 'Please enter required field.')),
            'customer_id' => new sfValidatorChoice(array('choices' => array_keys($customers), 'required' => true), array('invalid' => 'Invalid Customer Name.', 'required' => 'Please enter required field.')),
            'line_id' => new sfValidatorChoice(array('choices' => array_keys($lines), 'required' => true), array('invalid' => 'Invalid Line Name.', 'required' => 'Please enter required field.')),
            'amount' => new sfValidatorNumber(array('min' => 0, 'required' => true, 'max' => 99999999), array('min' => 'Invalid Amount.', 'invalid' => 'Invalid Amount.', 'max' => 'Invalid Amount.', 'required' => 'Please enter required field.')),
            'pay_type' => new sfValidatorChoice(array('choices' => array_keys(array('cheque'=>'Cheque', 'cash'=>'Cash', 'transfer'=>'Transfer')), 'required' => true), array( 'required' => 'Please enter required field.')),
            'date'         => new sfValidatorString(array('required' => true), array('invalid'=>'Invalid Date.','required'=>'Please enter required field.')),
            'due_date'         => new sfValidatorString(array('required' => $this->required), array('invalid'=>'Invalid Date.','required'=>'Please enter required field.')),
        ));
        
        $this->validatorSchema->setPostValidator(
                    new sfValidatorDoctrineUnique(
                            array('model' => 'Collection',
                                  'column' => array('cheque_number')),
                            array('invalid' => 'Cheque Number already exists. Please enter another one.')));
        if(!$this->code){
        $this->validatorSchema->setPostValidator(
                    new sfValidatorDoctrineUnique(
                            array('model' => 'Collection',
                                  'column' => array('collection_code')),
                            array('invalid' => 'Collection Code already exists. Please enter another one.')));
        }else{
        $this->validatorSchema->setPostValidator(
                    new sfValidatorDoctrineUniqueWithConditions(
                            array('model' => 'Collection',
                                  'column' => array('collection_code'),
                                  'conditions' => array('collection_code != '.$this->code)),
                            array('invalid' => 'Collection Code already exists. Please enter another one.')));
        }
        $this->widgetSchema->setNameFormat('collection[%s]');
        
    }

    private function getCustomersFormatted() {
        $bank_accounts = Doctrine_Query::create()->from('Customer')->where('is_deleted=?', 0)->orderBy('customer_name')->fetchArray();
        $result = array('' => '');

        foreach ($bank_accounts as $bank_account) {
            $result[$bank_account['id']] = $bank_account['customer_name'];
        }
        return $result;
    }
  
  private function getLinesFormatted() {
        $bank_accounts = Doctrine::getTable("Line")->findAll();
        $result = array('' => '');
        
        foreach ($bank_accounts as $bank_account) {
            $result[$bank_account['id']] = $bank_account['name'];
        }
        
        return $result;
    }
}

?>
